#!/bin/bash

# 日志审计功能测试脚本
# 用法: ./test_audit_log.sh

echo "=================================="
echo "  日志审计功能测试"
echo "=================================="
echo ""

# 配置
API_BASE="http://localhost:8000/api"
FRONTEND_URL="http://localhost:8080"

# 颜色输出
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 测试函数
test_api() {
    local name=$1
    local url=$2
    local method=${3:-GET}
    
    echo -n "测试: $name ... "
    
    if [ "$method" = "GET" ]; then
        response=$(curl -s -w "\n%{http_code}" "$url")
    else
        response=$(curl -s -w "\n%{http_code}" -X "$method" "$url")
    fi
    
    http_code=$(echo "$response" | tail -n1)
    
    if [ "$http_code" = "200" ] || [ "$http_code" = "401" ]; then
        echo -e "${GREEN}✓ 通过${NC} (HTTP $http_code)"
        return 0
    else
        echo -e "${RED}✗ 失败${NC} (HTTP $http_code)"
        return 1
    fi
}

# 检查服务状态
echo "1. 检查服务状态"
echo "-----------------------------------"

# 检查后端
if curl -s "$API_BASE/" > /dev/null 2>&1; then
    echo -e "${GREEN}✓${NC} 后端服务运行中: $API_BASE"
else
    echo -e "${RED}✗${NC} 后端服务未运行"
    exit 1
fi

# 检查前端
if curl -s "$FRONTEND_URL" > /dev/null 2>&1; then
    echo -e "${GREEN}✓${NC} 前端服务运行中: $FRONTEND_URL"
else
    echo -e "${YELLOW}!${NC} 前端服务可能未运行"
fi

echo ""

# 测试API端点
echo "2. 测试API端点"
echo "-----------------------------------"

test_api "审计日志列表" "$API_BASE/audit-logs/"
test_api "审计日志统计" "$API_BASE/audit-logs/statistics/"
test_api "主机巡检" "$API_BASE/host-inspection/" "POST"
test_api "当前用户" "$API_BASE/current-user/"

echo ""

# 检查数据库迁移
echo "3. 检查数据库"
echo "-----------------------------------"

cd /aiops/backend

migration_status=$(python3 manage.py showmigrations cmdb 2>&1 | grep "0005_auditlog")

if echo "$migration_status" | grep -q "\[X\]"; then
    echo -e "${GREEN}✓${NC} AuditLog迁移已应用"
else
    echo -e "${RED}✗${NC} AuditLog迁移未应用"
    echo "   运行: python3 manage.py migrate cmdb"
fi

# 检查模型
echo -n "检查AuditLog模型 ... "
if python3 -c "from cmdb.models import AuditLog; print('OK')" 2>/dev/null | grep -q "OK"; then
    echo -e "${GREEN}✓ 模型导入成功${NC}"
else
    echo -e "${RED}✗ 模型导入失败${NC}"
fi

echo ""

# 检查前端文件
echo "4. 检查前端文件"
echo "-----------------------------------"

frontend_files=(
    "/aiops/frontend/src/pages/AuditLog.vue"
    "/aiops/frontend/src/services/api.js"
    "/aiops/frontend/src/router/index.js"
    "/aiops/frontend/src/layouts/AppLayout.vue"
)

for file in "${frontend_files[@]}"; do
    if [ -f "$file" ]; then
        echo -e "${GREEN}✓${NC} $(basename $file)"
    else
        echo -e "${RED}✗${NC} $(basename $file) 缺失"
    fi
done

echo ""

# 检查路由配置
echo "5. 检查路由配置"
echo "-----------------------------------"

if grep -q "audit-log" /aiops/frontend/src/router/index.js; then
    echo -e "${GREEN}✓${NC} 前端路由已配置"
else
    echo -e "${RED}✗${NC} 前端路由未配置"
fi

if grep -q "audit-logs" /aiops/backend/cmdb/urls.py; then
    echo -e "${GREEN}✓${NC} 后端路由已配置"
else
    echo -e "${RED}✗${NC} 后端路由未配置"
fi

echo ""

# 使用说明
echo "6. 使用说明"
echo "-----------------------------------"
echo "访问日志审计页面:"
echo "  → $FRONTEND_URL/audit-log"
echo ""
echo "触发警告日志:"
echo "  1. 进入主机管理页面"
echo "  2. 点击任意主机的'主机详情'"
echo "  3. 点击'立即巡检'按钮"
echo "  4. 如果资源使用率超过90%，会自动记录警告"
echo ""
echo "查看完整文档:"
echo "  → /aiops/AUDIT_LOG_FEATURE.md"
echo "  → /aiops/AUDIT_LOG_QUICKSTART.md"
echo ""

# 总结
echo "=================================="
echo "  测试完成"
echo "=================================="
echo ""
echo -e "${GREEN}功能已实现:${NC}"
echo "  ✓ 审计日志数据模型"
echo "  ✓ 90%阈值自动警告"
echo "  ✓ 后端API接口"
echo "  ✓ 前端日志审计页面"
echo "  ✓ 路由和菜单配置"
echo ""
echo -e "${YELLOW}下一步:${NC}"
echo "  1. 登录系统: $FRONTEND_URL"
echo "  2. 进入: 系统管理 → 日志审计"
echo "  3. 执行主机巡检测试功能"
echo ""
